package org.hot100_3;

public class leetcode543 {
    public int diameterOfBinaryTree(TreeNode root) {
        return getDepth(root);
    }

    int maxDepth = 0;

    private int getDepth(TreeNode node) {
        if (node == null) {
            return 0;
        }

        int leftDepth = getDepth(node.left);
        int rightDepth = getDepth(node.right);

        int currDepth = Math.max(leftDepth,rightDepth)+1;

        maxDepth=Math.max(currDepth,leftDepth+rightDepth+1);

        return currDepth;
    }
}
